﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace SMP.Resource
{
    using SMP.Domain;
    public class Phieunhap_Res : BaseDB
    {
        Kho_Res _objKho = new Kho_Res();

        #region Phiếu nhập

        public DataTable GetAll_Phieunhap()
        {
            return ExecuteQuery("EXEC GetAll_Phieunhap ");
        }

        public DataTable Get_Phieunhap_Check_Used_By_Id_Phieunhap(long id_phieunhap)
        {
            string sql = @"EXEC Get_Phieunhap_Check_Used_By_Id_Phieunhap {0}";
            sql = string.Format(sql, id_phieunhap);
            return ExecuteQuery(sql);
        }

        public DataTable Get_Phieunhap_Check_Used_Xuathang_By_Id_Phieunhap(long id_phieunhap)
        {
            string sql = @"EXEC Get_Phieunhap_Check_Used_Xuathang_By_Id_Phieunhap {0} ";
            sql = string.Format(sql, id_phieunhap);
            return ExecuteQuery(sql);
        }

        public bool Delete_Phieunhap(object id_phieunhap)
        {
            string sql = string.Format(@"EXEC Delete_Phieunhap {0}", id_phieunhap);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Insert_Phieunhap(Phieunhap phieunhap)
        {
            string sql = string.Format("SET DATEFORMAT dmy; EXEC Insert_Phieunhap '{0}', '{1}', {2}, N'{3}' ",
                                                            phieunhap.Id_Nha_Cungcap, phieunhap.Ngaynhap, phieunhap.Tongtien, phieunhap.Ghichu);
            return ExecuteQuery(sql);
        }

        public bool Update_Phieunhap(Phieunhap phieunhap)
        {
            string sql = string.Format(@"SET DATEFORMAT dmy; UPDATE tb_phieunhap SET id_nha_cungcap = '{0}', ngaynhap = '{1}', tongtien = {2}, ghichu = N'{3}' WHERE id_phieunhap = '{4}'",
                                                            phieunhap.Id_Nha_Cungcap, phieunhap.Ngaynhap, phieunhap.Tongtien, phieunhap.Ghichu, phieunhap.Id_Phieunhap);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        #endregion

        #region Phiếu nhập chi tiết

        public DataTable GetAll_Mathang_By_Id_Phieunhap(object id_phieunhap)
        {
            string sql = string.Format(@"GetAll_Mathang_By_Id_Phieunhap {0} ", id_phieunhap);
            return ExecuteQuery(sql);
        }

        private object Insert_Phieunhap_Chitiet(Phieunhap phieunhap)
        {
            string sql = string.Format("EXEC Insert_Phieunhap_Chitiet '{0}', '{1}', {2}, '{3}', '{4}' ",
                                                                phieunhap.Id_Phieunhap, phieunhap.Id_Mathang, phieunhap.Soluong, phieunhap.Dongia, phieunhap.Thanhtien);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        private object Delete_Phieunhap_Chitiet(object id_phieunhap)
        {
            string sql = string.Format(@"EXEC Delete_Phieunhap_Chitiet {0}", id_phieunhap);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update_Phieunhap_Chitiet_Collection(DataTable dtb, object id_phieunhap, string action)
        {            
            try
            {
                if (action == "Insert")
                {
                    foreach (DataRow r in dtb.Rows)
                        Insert_Phieunhap_Chitiet(Get_Phieunhap_Chitiet(r, id_phieunhap));
                    _objKho.Update_Kho();
                }
                else
                {
                    Delete_Phieunhap_Chitiet(id_phieunhap);
                    foreach (DataRow r in dtb.Rows)
                        Insert_Phieunhap_Chitiet(Get_Phieunhap_Chitiet(r, id_phieunhap));
                    _objKho.Update_Kho();
                }

            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            return true;
        }

        private Phieunhap Get_Phieunhap_Chitiet(DataRow row, object id_phieunhap)
        {
            Phieunhap phieunhap = new Phieunhap();

            phieunhap.Id_Phieunhap = id_phieunhap;

            if (row["Id_Phieunhap_Chitiet"] + "" != "")
                phieunhap.Id_Phieunhap_Chitiet = row["Id_Phieunhap_Chitiet"];

            if(row["Id_Mathang"] + "" != "")
                phieunhap.Id_Mathang = row["Id_Mathang"];

            if(row["Soluong"] + "" != "")
                phieunhap.Soluong = row["Soluong"];

            if(row["Dongia"] + "" != "")
                phieunhap.Dongia = row["Dongia"];

            if(row["Thanhtien"] + "" != "")
                phieunhap.Thanhtien = row["Thanhtien"];
                        
            return phieunhap;
        }

        #endregion







    }
}
